<?php
namespace backend\models;

use Yii;
use yii\base\Model;
use yii\base\Exception;
use common\models\User;

class UserDataForm extends Model
{
	public $realname;
	public $username;
	public $time;
	public $status;

	public function formName()
    {
        return '';
    }

	public function rules()
	{
		return [
			[['realname', 'username', 'time', 'status'], 'safe']
		];
	}

	public function query()
	{
		$data = User::find()->where('id <> 0');
		if ($this->username) {
			$data = $data->andWhere(['like', 'username', $this->username]);
		}
		if ($this->realname) {
			$data = $data->joinWith('employee')->where(['like', 'employees.realname', $this->realname]);
		}
		if (!is_null($this->status)) {
			$data = $data->andWhere(['user.status' => $this->status]);
		}
		if ($this->time) {
			$re = explode('-', $this->time);
			$begin = strtotime(trim($re[0]));
			$end = strtotime(trim($re[1]));
			$data = $data->andWhere('user.created_at >= ' . $begin)->andWhere('user.created_at <= ' . $end);
		}

		return $data;
	}

}	